package com.duoec.dw.lineage.service.analyser.commend;

import com.duoec.dw.lineage.exceptions.LineageAnalyseException;
import com.duoec.dw.lineage.service.SqoopCommendLineAnalyser;

/**
 * @author xuwenzhen
 */
public abstract class BaseCommendLineAnalyser implements SqoopCommendLineAnalyser {
    protected static final String STR = "\"";
    public static final String STR_QUOT = "&quot;";

    protected String getString(String name, String line) {
        int index = line.indexOf(STR);
        int lastIndex = line.lastIndexOf(STR);
        if (index != -1 && lastIndex != -1) {
            return line.substring(index + 1, lastIndex);
        }

        //尝试转义字符
        index = line.indexOf(STR_QUOT);
        lastIndex = line.lastIndexOf(STR_QUOT);
        if (index != -1 && lastIndex != -1) {
            return line.substring(index + STR_QUOT.length(), lastIndex);
        }
        throw new LineageAnalyseException("无法解析" + name + ": " + line);
    }
}
